# $Id: hooks.py c3c2d6ebfe9b 2012/12/05 00:51:39 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy  <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

__version__  = "$Revision: c3c2d6ebfe9b $"
__author__   = "$Author: pgurumur $"
__modified__ = "$Date: 2012-12-05 00:51:39Z $"

import datetime, os, sys, shutil, subprocess
from mercurial.node import bin, short

def hook(ui, repo, hooktype, node=None, url=None, **kwds):
   nodeInfo = bin(node)
   if hooktype == "commit":
      cvsrepo = "%s/working/cvs" %os.environ['HOME']
      hgrepo = "%s/working/hg" %os.environ['HOME']
      context = repo.changectx(nodeInfo)
      author = context.user()
      revision = '%d:%s' % (context.rev(), short(context.node()))
      message = context.description()
      date = datetime.date.fromtimestamp(
            context.date()[0]).strftime("%A %b %d %I:%M:%S %p %Y")
      parent = context.parents()[0]
      oldrevision = '%d:%s' % (parent.rev(), short(parent.node()))

      files = repo.status(parent.node(), context.node())
      changed = [fi for fi in files[0]]
      added = [fi for fi in files[1]]
      removed = [fi for fi in files[2]]

      message = "hg2cvs mesg: %s, hg revision: %s, by %s" %(
            message, revision, author)
      os.chdir(hgrepo)
      for ix in changed:
         cvsfile = "%s/%s" %(cvsrepo, ix)
         if os.path.exists(cvsfile):
            shutil.copyfile(ix, cvsfile)
         else:
            added.append(ix)

      error = []
      for ix in added:
         os.chdir(hgrepo)
         cvsfile = "%s/%s" %(cvsrepo, ix)
         try:
            os.makedirs(os.path.dirname(cvsfile), 0755)
         except OSError as err:
            if err.errno == 17:
               shutil.copyfile(ix, cvsfile)
               os.chdir(cvsrepo)
               command = "cvs add %s" %ix
               retval = subprocess.call(command, shell = True)
            else:
               if ix not in err:
                  error.append(ix)
         else:
            directory = "%s/CVS" %os.path.dirname(cvsfile)
            if not os.path.exists(directory):
               command = "cvs add %s" %os.path.dirname(cvsfile)
               retval = subprocess.call(command, shell = True)
            else:
               print("%s already under cvs, not trying to add" %os.path.dirname(
                  cvsfile))

            shutil.copyfile(ix, cvsfile)
            os.chdir(cvsrepo)
            command = "cvs add %s" %ix
            retval = subprocess.call(command, shell = True)

      files = changed + added
      jy = ""
      for ix in files:
         if ix not in error:
            jy += ix + " "
         else:
            print("%s is part of error" %ix)

      os.chdir(cvsrepo)
      command = "cvs ci -m '%s' %s" %(message, jy)
      retval = subprocess.call(command, shell = True)
